package com.cmos.um.web;

import java.text.MessageFormat;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.cmos.um.exception.DescribeException;
import com.cmos.um.exception.ServErrorCode;
import com.cmos.um.iservice.ICallRecordSV;
import com.cmos.um.utils.Result;
import com.cmos.um.utils.http.RestClientHttpRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping({"/cin-cc"})
public class RecordController {
    private static final Logger logger = LoggerFactory.getLogger(RecordController.class);
    @Value("${media.defaultHostName}")
    private String defaultHostName;
    @Value("${media.msServerPattern}")
    private String msServerPattern;
    @Autowired
    private RestClientHttpRequest client;

    public RecordController() {
    }

    @ResponseBody
    @GetMapping({"/ctiRecordInfo.do"})
    public ResponseEntity download(String corpVccId, String fileName, String msServer) {
        if (StringUtils.isEmpty(msServer)) {
            msServer = this.defaultHostName;
        }

        String filePath;
        if (StringUtils.isEmpty(corpVccId)) {
            filePath = MessageFormat.format(ServErrorCode.PARAMS_EMPTY_ERROR.getMsg(), "corpVccId");
            logger.error(filePath);
            throw new DescribeException(ServErrorCode.PARAMS_EMPTY_ERROR.getStatus(), filePath);
        } else if (StringUtils.isEmpty(fileName)) {
            filePath = MessageFormat.format(ServErrorCode.PARAMS_EMPTY_ERROR.getMsg(), "fileName");
            logger.error(filePath);
            throw new DescribeException(ServErrorCode.PARAMS_EMPTY_ERROR.getStatus(), filePath);
        } else {
            filePath = fileName.replace("*", "/");
            String targetUrl = MessageFormat.format(this.msServerPattern, msServer, corpVccId, filePath);
            if (logger.isInfoEnabled()) {
                logger.info("ctiRecordInfo.do: request param is : corpVccId is -->{}, fileName is -->{}, msServer is -->{}. targetUrl is -->{}  ", new Object[]{corpVccId, fileName, msServer, targetUrl});
            }

            HttpHeaders headers = new HttpHeaders();
            ResponseEntity response = null;

            try {
                response = this.client.doExchange(targetUrl, HttpMethod.GET, headers, (Object)null, byte[].class);
                if (logger.isInfoEnabled()) {
                    logger.info("media:　call /cin-cc/ctiRecordInfo.do interface success. request targetUrl is -->{}, response is -->{} ", targetUrl, response.getStatusCode());
                }

                return response;
            } catch (Exception var9) {
                logger.error("ctiRecordInfo.do: request fail, fail reason is -->{}.request param is : corpVccId is -->{}, fileName is -->{}, msServer is -->{}. targetUrl is -->{}", new Object[]{var9.getCause(), corpVccId, fileName, msServer, targetUrl});
                return new ResponseEntity(new byte[0], HttpStatus.NOT_FOUND);
            }
        }
    }

}
